package com.mayblackcat.dy.controller;

import com.mayblackcat.dy.FrontEndData.AjaxResult;
import com.mayblackcat.dy.constant.Constants;
import com.mayblackcat.dy.entity.User;
import com.mayblackcat.dy.entity.Video;
import com.mayblackcat.dy.service.UserService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/login")
public class UserController {
    @Autowired
    private UserService userService;



    //修改
    @PostMapping(value = "/modification")
    public AjaxResult modifyUser(@RequestBody User user, HttpSession session){

        System.out.println(user);
        //重新设置session
        session.setAttribute("user",user);
        User user1= (User) session.getAttribute("user");
        userService.updateUserAll(user);
        AjaxResult ajaxResult=new AjaxResult(AjaxResult.Type.SUCCESS,"success",user1);
        return ajaxResult;
    }

    //返回用户观看历史
    @GetMapping("/watchHistory")
    public AjaxResult userWatchHistory(HttpSession session){
        User curUser=(User) session.getAttribute("user");
        String ids=curUser.getWatchVideo();
        List<Video> videos=userService.getWatchVideoByIds(ids);
        AjaxResult ajaxResult=new AjaxResult(AjaxResult.Type.SUCCESS,"success",videos);
        return ajaxResult;
    }
}
